package com.abu.vodms.controller;


import com.abu.vodms.service.VodService;
import com.abu.vodms.template.InitialVodClient;
import com.abu.exceptionhandler.EduException;
import com.abu.utils.Resp;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.baomidou.mybatisplus.extension.api.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 视频点播接口
 */
@RestController
@RequestMapping("vodms/video")
public class VodController {

    @Value("${vod.accessKeyId}")
    private String accessKeyId;

    @Value("${vod.accessKeySecret}")
    private String accessKeySecret;

    @Autowired
    private VodService vodService;

    @PostMapping("uploadAlyiVideo")
    public Resp vodUpload(MultipartFile file) {
        String videoId = this.vodService.uploadVideo(file);
        if (StringUtils.isNotBlank(videoId)) {
            return Resp.ok().data("videoId", videoId);
        }
        return Resp.error();
    }

    /**
     * 根据视频id删除阿里云的视频资源
     *
     * @param
     * @return
     */
    @DeleteMapping("removeAlyVideo/{id}")
    public Resp removeAlyVideo(@PathVariable("id") String vid) {
        DefaultAcsClient client = null;
        try {
//            1.初始化对象
            client = InitialVodClient.initVodClient(this.accessKeyId, this.accessKeySecret);
//            2.创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
//            3.设置删除的视频的id（删除多个，字符串以,进行分隔）
            request.setVideoIds(vid);
//            4.调用初始化对象实现删除
            client.getAcsResponse(request);
            return Resp.ok();
        } catch (ClientException e) {
            e.printStackTrace();
            throw new EduException(20001, "删除云端视频失败");
        }
    }


    //根据视频id获取视频凭证
    @GetMapping("getPlayAuth/{id}")
    public Resp getPlayAuth(@PathVariable String id) {
        DefaultAcsClient client = null;
        String playAuth = null;
        try {
            //创建初始化对象
            client = InitialVodClient.initVodClient(this.accessKeyId, this.accessKeySecret);
            //创建获取凭证request和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //向request设置视频id
            request.setVideoId(id);
            //调用方法得到凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            playAuth = response.getPlayAuth();
        } catch (Exception e) {
            throw new EduException(20001, "获取凭证失败");
        }
        return Resp.ok().data("playAuth", playAuth);
    }
}
